﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyThuVien.Business;

namespace QuanLyThuVien.Presentation
{
    public partial class frmQuanLyMuonSach : Form
    {
        public frmQuanLyMuonSach()
        {
            InitializeComponent();
        }

        DauSachBUS busSach = new DauSachBUS();
        TheBUS busThe = new TheBUS();
        SachBUS BusDS = new SachBUS();
        DocGiaBUS BusDG = new DocGiaBUS();
        PhieuMuonBUS BusPhieumuon = new PhieuMuonBUS();
        PhieuMuon Phieumuon = new PhieuMuon();

        private void btTra_Click(object sender, EventArgs e)
        {
            frmQuanLyTraSach frm = new frmQuanLyTraSach();
            frm.ShowDialog();
        }

        private void frmQuanLyTraMuon_Load(object sender, EventArgs e)
        {
            BusPhieumuon.LoadLenDataGridView(dgMuon);
            busSach.LoadDLLenCombobox(cbTenSachM);
            busThe.LoadMaTheLenCB(cbMaThe);
        }

        private void cbTenSachM_SelectedIndexChanged(object sender, EventArgs e)
        {
            busSach.LayChuyenNganhLenCB(cbChuyenNganh, cbTenSachM.Text);
        }

        private void cbChuyenNganh_SelectedIndexChanged(object sender, EventArgs e)
        {
            busSach.LayMaDSLenCBTheoCN(cbMaDS,cbChuyenNganh.Text,cbTenSachM.Text);
        }

        private void cbMaDS_SelectedIndexChanged(object sender, EventArgs e)
        {
            BusDS.LayMaSachLenCB(cbMaSach, cbMaDS.Text);
        }

        private void cbMaThe_SelectedIndexChanged(object sender, EventArgs e)
        {
          lbTenDG.Text= BusDG.LaodTenDGLenLable(cbMaThe.Text);
        }

        private void btMuon_Click(object sender, EventArgs e)
        {
            try
            {
                Phieumuon.MaSach = cbMaSach.Text;
                Phieumuon.MaThe = cbMaThe.Text;
                Phieumuon.NgayMuon = mkNgayMuon.Text;
                Phieumuon.NgayTra2 = "  /  /";
                BusPhieumuon.ThemMoiPhieuMuon(Phieumuon);
                BusPhieumuon.LoadLenDataGridView(dgMuon);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "QuanLyThuVien");
            }
        }

        private void btSua_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có thực sự muốn sửa thông tin hay không?", "QuanLyThuVien", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                Phieumuon.MaSach = cbMaSach.Text;
                Phieumuon.MaThe = cbMaThe.Text;
                Phieumuon.NgayMuon = mkNgayMuon.Text;
                Phieumuon.NgayTra2 ="  /  /";
                BusPhieumuon.SuaPhieuMuon(Phieumuon);
                BusPhieumuon.LoadLenDataGridView(dgMuon);
            }
        }

        private void btnTimKiemNhanh_Click(object sender, EventArgs e)
        {
            frmTKSach frm = new frmTKSach();
                frm.ShowDialog();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có thực sự muốn xóa thông tin hay không?", "QuanLyThuVien", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                Phieumuon.MaSach = cbMaSach.Text;
                Phieumuon.MaThe = cbMaThe.Text;
                Phieumuon.NgayMuon = mkNgayMuon.Text;
                Phieumuon.NgayTra2 = "  /  /";
                BusPhieumuon.XoaPhieuMuon(Phieumuon);
                BusPhieumuon.LoadLenDataGridView(dgMuon);
                foreach (Control cl in groupBox1.Controls)
                {
                    if (cl is ComboBox)
                        cl.Text = "";
                    if (cl is MaskedTextBox)
                        cl.Text = "";
                }
            }
        }

        private void dgMuon_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int a = e.RowIndex;
            cbTenSachM.Text=dgMuon[0,a].Value.ToString();
            cbChuyenNganh.Text=dgMuon[1,a].Value.ToString();
            cbMaDS.Text=dgMuon[2,a].Value.ToString();
            cbMaSach.Text=dgMuon[3,a].Value.ToString();
            cbMaThe.Text=dgMuon[4,a].Value.ToString();
            lbTenDG.Text=dgMuon[5,a].Value.ToString();
            mkNgayMuon.Text = dgMuon[6, a].Value.ToString();
            //mkNgayTra.Text=dgMuon[7,a].Value.ToString();
        }

        private void btnTaoMoi_Click(object sender, EventArgs e)
        {
            cbTenSachM.Text = "";
            cbChuyenNganh.Text = "";
            cbMaDS.Text = "";
            cbMaSach.Text = "";
            cbMaThe.Text = "";
            lbTenDG.Text = "";
            mkNgayMuon.Text = "";
        }

    }
}
